#include "stdafx.h"
#include "SendMessageErrorTestSuite.h"
#include "TestPayload.h"

namespace CO { namespace SpiFoundation { namespace ErrorHandling {

using namespace RoomFW;

SendMessageErrorTestSuite::SendMessageErrorTestSuite(void)
{
	TEST_ADD(SendMessageErrorTestSuite::CtorMtor)
	TEST_ADD(SendMessageErrorTestSuite::ReasonText)
}

void SendMessageErrorTestSuite::setup()
{
}

void SendMessageErrorTestSuite::tear_down()
{
}

void SendMessageErrorTestSuite::CtorMtor(void)
{
	//
	// Test constants.
	//
	const ObjectId myObjectId = 42;
	const char* myObjectName = "myObjectName";
	const PortIndex myPortIndex(42, 42);
	const MessageID myMessageId = 42;
	const TestPayloadSP payload(new TestPayload(42));
	const SendMessageError::Reason reason = SendMessageError::SMER_CONTRACT_VIOLATION;

	//
	// Construct some test objects.
	//
	SendMessageError e1(
						myObjectName,
						myPortIndex,
						myMessageId,
						payload,
						reason);
	SendMessageError e2(myPortIndex,
						myMessageId,
						payload,
						reason);

	//
	// Test Constructor values returned by Accessors
	//
	TEST_ASSERT(e1.getId() == EID_FND_SEND_MESSAGE);
	TEST_ASSERT(e2.getId() == EID_FND_SEND_MESSAGE);

	TEST_ASSERT(e1.getSeverity() == ESEVsoftwareWarning);
	TEST_ASSERT(e2.getSeverity() == ESEVsoftwareWarning);

	TEST_ASSERT(e1.getSourceObjectName() == myObjectName);
	TEST_ASSERT(e2.getSourceObjectName() == "");

	TEST_ASSERT(e1.getPortIndex() == myPortIndex);
	TEST_ASSERT(e2.getPortIndex() == myPortIndex);

	TEST_ASSERT(e1.getMessageId() == myMessageId);
	TEST_ASSERT(e2.getMessageId() == myMessageId);

	TestPayloadSP e1Payload = ::boost::dynamic_pointer_cast<TestPayload, IPayload>(e1.getPayload());
	TestPayloadSP e2Payload = ::boost::dynamic_pointer_cast<TestPayload, IPayload>(e2.getPayload());
	TEST_ASSERT(0 != e1Payload);
	TEST_ASSERT(0 != e2Payload);
	TEST_ASSERT(e1Payload->value() == payload->value());
	TEST_ASSERT(e2Payload->value() == payload->value());

	//
	// Test error text.
	//
	const char* eTxt1 = "(EID_FND_SEND_MESSAGE[5]:ESEVsoftwareWarning:myObjectName)CONTRACT_VIOLATION [42[42], 42], payload=TestPayload: value=42";
	const char* eTxt2 = "(EID_FND_SEND_MESSAGE[5]:ESEVsoftwareWarning:)CONTRACT_VIOLATION [42[42], 42], payload=TestPayload: value=42";

	std::string msg;
	msg = "Expected: <" + std::string(eTxt1) + ">, Got: <" + std::string(e1.what()) + ">";
	TEST_ASSERT_MSG(::strstr(e1.what(), eTxt1), msg.c_str());

	msg = "Expected: <" + std::string(eTxt2) + ">, Got: <" + std::string(e2.what()) + ">";
	TEST_ASSERT_MSG(::strstr(e2.what(), eTxt2), msg.c_str());

	//
	// Test Mutators
	//
	e2.setObjectName(myObjectName);
	TEST_ASSERT(e2.getSourceObjectName() == myObjectName);
	TEST_ASSERT(::strstr(e2.what(), eTxt1));
}

void SendMessageErrorTestSuite::ReasonText()
{
	static const char* rTxt[] =
	{"NONE",
	"INVALID_PORT",
	"PORT_NOT_BOUND",
	"RECEIVER_ERRORED",
	"BROADCAST_PAYLOAD",
	"BROADCAST_INVOKE",
	"CONTRACT_VIOLATION",
	"REPLY_TO_REPLY",
	"INVOKE_TIMEOUT"};

	for (size_t i = 0; i < SendMessageError::SMER_COUNT; ++i)
	{
		TEST_ASSERT(0 == ::strcmp(SendMessageError::ReasonText((SendMessageError::Reason)i), rTxt[i]));
	}
}

}}}
